#coding: utf-8
# Play credits

import pygame
from pygame.locals import *
import sys,os

import sound
import popup
import animation
import random

def run(screen):
    sound.play_music("robot_ending_arr.ogg", 0)
    
    background = pygame.Surface(screen.get_size())
    background.fill((0,0,0))
    trans_layer = pygame.Surface(screen.get_size())
    trans_layer.fill((0,0,0))
    trans_layer.set_alpha(200)

    rect = screen.get_rect()

    dance = animation.Animation(os.path.join("robot", "tanzen"))
    
    dancer = dance.image
    dancer_rect = dance.rect

    for (text, time) in (([""], 1),
                         (["Ghetto Viking:", "The Prophecy"], 6),
                         ([""], 2),
                         (["A game by:",
                           u"Jonne Mickelin     Jakob Florell      Eric Hörberg", 
                           u"Christoffer Edström      Dag Palocci Staffansson"], 
                          4),
                         ([""], 2),
                         ([u"Idea:", "Jonne Mickelin     Jakob Florell      Eric Hörberg"], 3),
                         ([""], 2)):
        panel = popup.popup(text, (179, 130, 36), None, 30)
        start_time = pygame.time.get_ticks()

        while pygame.time.get_ticks() < start_time + 1000*time: 
            for event in pygame.event.get():
                if event.type == QUIT:
                    sys.exit(0)
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        return

            text_pos = (rect.centerx - panel.get_rect().w/2, rect.centery - panel.get_rect().h/2)
            dance_pos = (rect.centerx - dancer.get_rect().w/2, rect.centery  - dancer.get_rect().h/2 + 100)
            screen.blit(background, (0,0))
            
            screen.blit(dancer, dance_pos)
            screen.blit(trans_layer, (0,0))
            
            screen.blit(panel, text_pos)        
            
            pygame.display.flip() 
        
    #twice? meh
    for (text, time) in ((["Progamming:","Jonne Mickelin     Jakob Florell"], 4),
                         ([""], 1),
                         ([u"Level design:", "Eric Hörberg"], 4),
                         ([""], 1),
                         (["Graphics:"], 4),
                         ([""], 1),
                         ([u"Sprites:","Christoffer Edström       Dag Palocci"],  4),
                         ([""], 1),
                         (["Tile art:", "Dag Palocci"], 7),
                         ([""], 1),
                         (["Music:", "CP80"], 6),
                         ([""], 2),
                         (["Join in to the Robot Krazy Tanze!"], 2),
                         ([""], 16),
                         (["Thanks to everyone not mentioned"], 10),
                         ([""], 45)): #to long to finish
        
        panel = popup.popup(text, (179, 130, 36), None, 30)
        start_time = pygame.time.get_ticks()
        times = 0

        while pygame.time.get_ticks() < start_time + 1000*time: 
            for event in pygame.event.get():
                if event.type == QUIT:
                    sys.exit(0)
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        return

            
            if pygame.time.get_ticks() > start_time + 300 * times:
                rand_x = random.randint(0, rect.w - dancer_rect.w/2)
                rand_y = random.randint(0, rect.h - dancer_rect.h/2)
                times += 1
                dance_pos = (rand_x, rand_y)
            if pygame.mixer.music.get_pos() > 34000: #Start the KRAZY DANZZZZZZE!
                next = dance.next()
                if next is not None:
                    dancer = next[0]

            if not pygame.mixer.music.get_busy():
                return

            text_pos = (rect.centerx - panel.get_rect().w/2, rect.centery - panel.get_rect().h/2)

            screen.blit(background, (0,0))

            screen.blit(dancer, dance_pos)
            screen.blit(trans_layer, (0,0))
            screen.blit(panel, text_pos)
            pygame.display.flip() 
        

        
